package com.mygdx.game_world;

import java.util.ArrayList;

import com.badlogic.gdx.math.Intersector;
import com.mygdx.game_objects.Basket;
import com.mygdx.game_objects.Bird;
import com.mygdx.game_objects.Eggs;

public class GameWorld {

	// private Birds bird;
	private ArrayList<Bird> birds;
	private Basket basket;
	private int gameWidth, gameHeight;
	private int live;
	private int score;
	private boolean isCollide = false;

	public GameWorld(int gameWidth, int gameHeight) {
		this.gameWidth = gameWidth;
		this.gameHeight = gameHeight;
		birds = new ArrayList<Bird>();
		int basketWidth = 70;
		basket = new Basket(gameWidth / 2 - basketWidth / 2, gameHeight - 70, basketWidth, 40, gameWidth);
		live = 3;
		score = 0;
	}

	public void update(float delta) {
		if (birds.size() > 0) {
			for (Bird b : birds) {
				b.update(delta);
				if (b.isDropped()) {
					if (catchEgg(b)) {
						b.getEgg().setCatched(true);
						if (b.getEgg().isShit()) {
							badLive();
						} else {
							score++;
							System.out.println(score);
							if (score % 20 == 0)
								Eggs.fallSpeed = Eggs.fallSpeed * 1.5f; // +50% speed after every 20 eggs earned
						}
					}
					if (b.getEgg().isFallOut()) {
						badLive();
					}
				}
			}
		}
		basket.update(delta);
	}

	private void badLive() {
		// TODO Auto-generated method stub
		live--;
		if (live == 0) {
			gameOver();
		}
	}

	private void gameOver() {
		// TODO Auto-generated method stub

	}

	public void callBird(String type) {
		Bird bird = new Bird(type, gameWidth / 2, 40, 45, 60, gameWidth, gameHeight);
		birds.add(bird);
	}

	public boolean catchEgg(Bird b) {
		if (b.getEgg().getEggShape().y + b.getEgg().getEggShape().radius > basket.getY()) {
			if (b.getEgg().getEggShape().x > basket.getX() && b.getEgg().getEggShape().x < basket.getTailX()
					&& !b.getEgg().isCatched()) {
				return true;
			}
		}
		return false;
	}

	public ArrayList<Bird> getBirds() {
		return birds;
	}

	public Basket getBasket() {
		return basket;
	}

}
